import { INIT_PANELS, INIT_GRIDS, INIT_MODALS, CLOSE_MODALS, RESET_MODALS, SET_MODALS_PARAM } from '../action/comAction';

/*
*  Panels信息状态
*  @param openPanels {Array} 打开的Tab 页面组
*  @param panelsReducer {Function} 管理Tab状态函数
*
* */

const Panels = {
  openPanels: [],
  activeKey: null
};

const Grids = {
  param: {},
  fetchDataHandler: null,
  cotext: null
}

const Modals =  {
  key: '',
  visible: !1,
  param: {},
  fetchParams: null,
  render: !0, // 是否进行渲染
  context: null
}

const panelsReducer = (state = Panels, action) => {
  switch (action.type) {
    case INIT_PANELS:
      return {
        ...state,
        openPanels: [
          ...Panels.openPanels,
          ...action.tab
        ],
        activeKey: action.activeKey
      };
    default:
      return state;
  }
};

const gridsReducer = (state = Grids, action) => {
  switch (action.type) {
    case INIT_GRIDS:
      return {
        ...state,
        param: action.param,
        fetchDataHandler: action.handler,
        context: action.context
      }
    default:
      return state;
  }
}

const modalsReducer = (state = Modals, action) => {
  switch (action.type) {
    case INIT_MODALS:
      return {
        ...state,
        key: action.key,
        visible: action.visible,
        param: action.param,
        render: !0,
        context: action.context
      }
    case CLOSE_MODALS:
      return {
        ...state,
        render: !0,
        visible: action.visible
      }
    case RESET_MODALS:
      return {
        ...state,
        key: action.key,
        render: !1,
        fetchParams: action.handler
      }
    case SET_MODALS_PARAM:
      return {
        ...state,
        key: action.key,
        render: !1,
        param: action.param
      }
    default:
      return state;
  }
}

const comReducer = {
  'panelsInfo': panelsReducer,
  'gridsInfo': gridsReducer,
  'modalInfo': modalsReducer
}

export default comReducer;